home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcr / pcr4_4.lha / DIST / dylibload / LibrarySearchingLoad.c next >
C/C++ Source or Header  |  1992-04-16  |  6KB  |  218 lines

  1. /* begincopyright
  2.   Copyright (c) 1988-1991 Xerox Corporation. All rights reserved.
  3.   Use and copying of this software and preparation of derivative works based
  4.   upon this software are permitted. Any distribution of this software or
  5.   derivative works must comply with all applicable United States export
  6.   control laws. This software is made available AS IS, and Xerox Corporation
  7.   makes no warranty about the software, its performance or its conformity to
  8.   any specification. Any person obtaining a copy of this software is requested
  9.   to send their name and post office or electronic mail address to:
  10.     PCR Coordinator
  11.     Xerox PARC
  12.     3333 Coyote Hill Rd.
  13.     Palo Alto, CA 94304
  14.   endcopyright */
  15.  
  16. /*
  17.  * LibrarySearchingLoad.c
  18.  *
  19.  * Demers, January 17, 1991 4:59:28 pm PST
  20.  */
  21.  
  22. #include "xr/BasicTypes.h"
  23. #include "xr/IncrementalLoad.h"
  24. #include "xr/IncrementalLoadPrivate.h"
  25. #include "xr/CommandLine.h"
  26. #include "xr/CommandLoop.h"
  27. #include "xr/ThreadsMsg.h"
  28. #include "xr/UIO.h"
  29.  
  30. #include "xr/libsearch.h"
  31.  
  32.  
  33. typedef struct XR_PrintUndefinedDataRep {
  34.     bool pud_firstEntry;
  35.     XR_CLCallEnv pud_clce;
  36. } *XR_PrintUndefinedData;
  37.  
  38.  
  39. static bool
  40. XR_LSLPrintUndefinedSymbol(se, clientData)
  41.     XR_ILSymEntry se;
  42.     XR_Pointer clientData;
  43. {
  44. #   define pud ((XR_PrintUndefinedData)(clientData))
  45.     XR_CLCallEnv clce = pud->pud_clce;
  46.  
  47.     if( pud->pud_firstEntry ) {
  48.         XR_CLErrorMsg "Undefined:\n");
  49.         pud->pud_firstEntry = FALSE;
  50.     }
  51.     XR_CLErrorMsg "    %s", se->ilse_name);
  52.     if( se->ilse_ilfe != NIL ) {
  53.         XR_CLErrorMsg
  54.                 " (referenced from %s)", se->ilse_ilfe->ilfe_fName);
  55.     }
  56.     XR_CLErrorMsg "\n");
  57.     return TRUE;
  58. }
  59. #undef pud
  60.  
  61.  
  62. static bool
  63. XR_LSLGetNextUndefinedSymbol(se, clientData)
  64.     XR_ILSymEntry se;
  65.     XR_Pointer clientData;
  66. {
  67. #   define resP ((XR_ILSymEntry *)(clientData))
  68.  
  69.     *resP = se;
  70.     return FALSE;
  71. }
  72.  
  73. static void
  74. XR_LibLoadCommandPatchSizeProc(ilfe, bytes)
  75.     XR_ILFileEntry ilfe;
  76.     unsigned bytes;
  77. {
  78.     ilfe->ilfe_pBytes = bytes;
  79. }
  80.  
  81.  
  82. static
  83. XR_CLPROC(XR_CLProc_librarySearchingLoad)
  84. {
  85.     XR_ILError ile = NIL;
  86.     XR_ILError ile2;
  87.     XR_ILSymEntry nextUndefinedSymbol;
  88.     bool symFoundInLibrary;
  89.     XR_Fildes fd;
  90.     unsigned offset;
  91.     unsigned magic;
  92.     char *modulename;
  93.     char *cp;
  94.     int i;
  95.     void (*patchSizeProc)() = NIL;
  96.     long int patchBytes = 0;
  97.  
  98.     ile = XR_LockIncrementalLoadState(TRUE);
  99.     if( ile != NIL ) {
  100.         XR_CLErrorMsg "%s: error %d: %s\n",
  101.             argv[0], ile->ile_code, ile->ile_msg);
  102.         return (-1);
  103.     }
  104.  
  105.     for( i = 1; i < argc; i++ ) {
  106.         if( (argv[i][0] == '-') && (argv[i][1] == 'p') ) {
  107.             patchBytes = atoi(&(argv[i][2]));
  108.             if( patchBytes > 0 ) {
  109.                 patchSizeProc = XR_LibLoadCommandPatchSizeProc;
  110.             } else {
  111.                 if( patchBytes < 0 )
  112.                     XR_CLLogMsg "bad flag %s ignored ... ", argv[i]);
  113.                 patchSizeProc = NIL;
  114.                 patchBytes = 0;
  115.             }
  116.             continue;
  117.         }
  118.         XR_CLLogMsg "loading %s ... ", argv[i]);
  119.         ile = XR_ILLoadFile(
  120.             /*fName*/            argv[i],
  121.             /*fOffset*/            0,
  122.             /*fMagic*/            0,
  123.             /*refProc*/            NIL,
  124.             /*refClientData*/        NIL,
  125.             /*defProc*/            NIL,
  126.             /*defClientData*/        NIL,
  127.             /*commonProc*/        NIL,
  128.             /*commonClientData*/    NIL,
  129.             /*patchSizeProc*/        patchSizeProc,
  130.             /*patchSizeClientData*/    patchBytes
  131.         );
  132.         if( ile != NIL ) {
  133.             XR_CLLogMsg "error %d: %s\n", ile->ile_code, ile->ile_msg);
  134.             break;
  135.         }
  136.         XR_CLLogMsg "ok\n");
  137.     }
  138.  
  139.  
  140.     for(;;) {
  141.         if( ile != NIL ) break;
  142.         ile = XR_CommitIncrementalLoad();
  143.         if( ile == NIL ) break;
  144.         nextUndefinedSymbol = NIL;
  145.         XR_ILEnumerateUndefinedSyms(
  146.                 XR_LSLGetNextUndefinedSymbol,
  147.                 ((XR_Pointer)(&nextUndefinedSymbol)) );
  148.         if( nextUndefinedSymbol == NIL ) /* ile still not nil */ break;
  149.         if( (nextUndefinedSymbol->ilse_type & ILSE_TYPE) != ILSE_UNDF ) {
  150. XR_ConsoleMsg("nextUndefinedSymbol botch name %s type 0x%x\n",
  151. nextUndefinedSymbol->ilse_name, nextUndefinedSymbol->ilse_type );
  152.             break;
  153.         }
  154.         XR_CLLogMsg "searching for %s in library ... ",
  155.                 nextUndefinedSymbol->ilse_name );
  156.         symFoundInLibrary = XR_lib_symfind( nextUndefinedSymbol->ilse_name,
  157.                 &fd, &offset, &magic, &modulename );
  158.         if( !symFoundInLibrary ) {
  159.             XR_CLLogMsg "not found\n");
  160.             break;
  161.         }
  162.         for (cp = modulename; (*cp != 0) && (*cp != '('); cp++) {};
  163.         XR_CLLogMsg "found in %s\n", cp);
  164.         *cp = 0;
  165.         ile = XR_ILLoadFile(
  166.             /*fName*/            modulename,
  167.             /*fOffset*/            offset,
  168.             /*fMagic*/            0,
  169.             /*refProc*/            NIL,
  170.             /*refClientData*/        NIL,
  171.             /*defProc*/            NIL,
  172.             /*defClientData*/        NIL,
  173.             /*commonProc*/        NIL,
  174.             /*commonClientData*/    NIL,
  175.             /*patchSizeProc*/        patchSizeProc,
  176.             /*patchSizeClientData*/    patchBytes
  177.         );
  178.     }
  179.     if( ile != NIL ) {
  180.         struct XR_PrintUndefinedDataRep pud;
  181.         pud.pud_firstEntry = TRUE;
  182.         pud.pud_clce = clce;
  183.         XR_ILEnumerateUndefinedSyms(
  184.                 XR_LSLPrintUndefinedSymbol,
  185.                 ((XR_Pointer)(&pud)) );
  186.         (void) XR_AbortIncrementalLoad(NIL);
  187.         (void) XR_UnlockIncrementalLoadState();
  188.         XR_CLErrorMsg "%s: error %d: %s\n",
  189.             argv[0], ile->ile_code, ile->ile_msg );
  190.         if( i < argc ) XR_CLErrorMsg "\tfile: %s\n", argv[i]);
  191.         return (-1);
  192.     } else {
  193.         (void) XR_UnlockIncrementalLoadState();
  194.         return 1;
  195.     }
  196. }
  197.  
  198.  
  199. char *(cmdDescription[]) = {
  200.     (char *)XR_CLProc_librarySearchingLoad, NIL,
  201.     "[-ppatchbytes] filename ... -- load object files (with library search)",
  202.     "load", "load_files", "libload_files", NIL,
  203.     NIL
  204. };
  205.  
  206.  
  207. int
  208. XR_run_LibrarySearchingLoad()
  209. {
  210.     int regAns;
  211.  
  212.     regAns = XR_CLUnsafeRegisterProcs(XR_globalCLProcsHandle, cmdDescription);
  213.     if( regAns < 0 ) {
  214. XR_ConsoleMsg("run_LibrarySearchingLoad FAILURE %d\n", regAns);
  215.         return -1;
  216.     }
  217. }
  218.